package com.runa.rccprotocol.dto.huanxiang;

import com.runa.protocol.dto.DtuRequestData;
import com.runa.protocol.dto.IRequestData;
import com.runa.protocol.util.ByteUtil;

import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * @author diandian
 * @Date 2022/5/20 9:34
 */
public class HuanXiangFmuRequestData extends DtuRequestData implements IRequestData<byte[]> {

    public HuanXiangFmuRequestData(String ip,short port ,String address, IRequestData<byte[]> requestData) {
        super(address, requestData);
        if (HuanXiangFmuRequestData.ip_port == null) {
            String[] ipArray = ip.split("\\.");
            ip_port = new byte[6];
            ip_port[0] = (byte) Integer.parseInt(ipArray[0]);
            ip_port[1] = (byte) Integer.parseInt(ipArray[1]);
            ip_port[2] = (byte) Integer.parseInt(ipArray[2]);
            ip_port[3] = (byte) Integer.parseInt(ipArray[3]);
            byte[] bytes = ByteUtil.shortToByte(port);
            ip_port[4] = bytes[0];
            ip_port[5] = bytes[1];
        }

    }

    private byte[] byteData;
    public static byte[] ip_port;


    public byte[] getData() {
        return this.byteData;
    }

    public void initRequest(int port,short baudRate,byte check) {
        //注：通道号为 0~6，其中当通道号为 0 时数据通过 RS485 传输，不为 0 时数据通过 Mbus 传输。波特
        //率高字节在前。如果有线终端无回复，则采集器回复空数据包的回复包，其中回复包控制码为异常代码。
        byte[] data = this.requestData.getData();
        this.byteData = new byte[26+data.length];
        int index = 0;
        this.byteData[index++] = (byte) 0xC0;
        this.byteData[index++] = 0x31;
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(0, 2), 16);
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(2, 4), 16);
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(4, 6), 16);
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(6, 8), 16);
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(8, 10), 16);
        this.byteData[index++] = (byte) Integer.parseInt(dtuID.substring(10, 12), 16);
        this.byteData[index++] = 0x02;
        this.byteData[index++] = 0;

        byte[] bytes = ByteUtil.shortToByte((short) (12 + data.length));
        this.byteData[index++] = bytes[1];
        this.byteData[index++] = bytes[0];

        for (int i = 0; i < ip_port.length; i++) {
            this.byteData[index++] = ip_port[i];
        }
        this.byteData[index++] = (byte) port;//通道号(1)

        byte[] baudBytes = ByteUtil.shortToByte(baudRate);
        this.byteData[index++] = baudBytes[1];//波特率(2)
        this.byteData[index++] = baudBytes[0];//波特率(2)
        this.byteData[index++] = 0x8;//数据位(1)
        this.byteData[index++] = 0x1;//停止位(1)
        this.byteData[index++] = check;//奇偶校验(1)

        for (int i = 0; i < data.length; i++) {
            this.byteData[index++] = data[i];
        }

        this.byteData[index++] = ByteUtil.sum(this.byteData, 1, this.byteData.length - 3);
        this.byteData[index++] = (byte) 0xD0;


//        this.byteData = ByteUtil.hexStringToByte("C0 31 03 00 40 22 06 35 02 00 00 22 C7 1C 0A 02 8F 1F 01 09 60 08 01 02 68 46 63 13 00 00 00 00 00 01 09 1F 90 01 34 20 09 07 06 16 5E 16 D9 D0".replace(" ", ""));
    }


    public static void main(String[] args) throws SocketException {
        System.out.println(ByteUtil.byteToHexString(ByteUtil.shortToByte((short) 2400)));
        HuanXiangFmuRequestData s = new HuanXiangFmuRequestData("199.28.10.2", (short) 8080, null, null);
        System.out.println(ByteUtil.byteToHexString(HuanXiangFmuRequestData.ip_port));

        String ss = "C031030040220635020014C71C0A028F1F02258008010202030001002695E3DAD0";
        byte[] bytes = ByteUtil.hexStringToByte(ss);
        byte sum = ByteUtil.sum(bytes, 1, bytes.length - 3);
        byte[] bytes1 = {sum};
        System.out.println(ByteUtil.byteToHexString(bytes1));

        System.out.println(ByteUtil.byteToHexString(ByteUtil.shortToByte((short) 9600)));


        HuanXiangEquRequestData equRequestData = new HuanXiangEquRequestData("030040202011");
        equRequestData.readHouseMeter_Modbus("00000000000154");
        System.out.println(ByteUtil.byteToHexString(equRequestData.getData()));
        HuanXiangFmuRequestData requestData = new HuanXiangFmuRequestData("192.168.1.1", (short) 2, "030040202011",equRequestData);
        requestData.initRequest(1,(short) 9600,(byte) 0x02);
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

    }

}